package com.zheng;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.inject.Named;

import net.sf.excelutils.ExcelException;
import net.sf.excelutils.ExcelUtils;

import org.dom4j.Element;

import tangdi.engine.context.Etf;
import tangdi.engine.context.Log;
import tangdi.engine.context.Msg;

public class Importcustommcwdc
{
  @Named("ToReportcustomcwdc")
  public static int ToReportcustomcwdc(@Named("Root") String Root, @Named("FileDemo") String FileDemo, @Named("FilePath") String FilePath, @Named("FileToPath") String FileToPath, @Named("NameForUser") String NameForUser, @Named("WebPath") String WebPath)
  {
    List reportList = null;

    reportList = iterlist2(Root);
    ExcelUtils.addValue("reportList", reportList);
    String config = WebPath + "/" + FilePath + "/" + FileDemo + ".xls";
    Log.info("config", new Object[] { config });
    String fileName = NameForUser + System.currentTimeMillis() + ".xls";
    Log.info("----fileName=%s--------", new Object[] { WebPath + FileToPath + "/" + fileName + config });
    try {
      ExcelUtils.export(config, new FileOutputStream(WebPath + FileToPath + "/" + fileName));
    }
    catch (FileNotFoundException e) {
      Log.info("----fileName=%s--", new Object[] { WebPath + FileToPath + "/" + fileName + "没找到" });
      return 1;
    }
    catch (ExcelException e) {
      Log.info("----fileName=%s--", new Object[] { WebPath + FileToPath + "/" + fileName + "导入异常" });
      return 2;
    }
    Msg.setTmp("FILENAME", fileName);
    return 0;
  }
  public static List iterlist2(String ROOT) {
    List list = Etf.childs(ROOT);
    Log.info("list的长度=%s", new Object[] { Integer.valueOf(list.size()) });
    List reportList = new ArrayList();

    for (int i = 0; i < list.size(); i++) {
      Log.info("----start=%s-----", new Object[] { "shabi" });
      
      Element em = (Element)list.get(i);
      
      Customcwdc user = new Customcwdc();
      Date date=new Date();
      DateFormat format=new SimpleDateFormat("yyyy年MM月dd");
      String tim=format.format(date);
      
      user.setMerno(em.element("MERNO") == null ? "无记录" : em.element("MERNO").getText());
      user.setCust_name(em.element("CUST_NAME") == null ? "无记录" : em.element("CUST_NAME").getText());
      user.setBank_name(em.element("BANK_NAME") == null ? "无记录" : em.element("BANK_NAME").getText());
      user.setCust_bank_deposit_name(em.element("CUST_BANK_DEPOSIT_NAME") == null ? "无记录" : em.element("CUST_BANK_DEPOSIT_NAME").getText());
      user.setCust_stl_bank_ac_no(em.element("CUST_STL_BANK_AC_NO") == null ? "无记录" : em.element("CUST_STL_BANK_AC_NO").getText());
      user.setSum_ordamt(em.element("SUM_ORDAMT") == null ? "无记录" : em.element("SUM_ORDAMT").getText());
      user.setSum_comant(em.element("SUM_COMANT") == null ? "0" : em.element("SUM_COMANT").getText());
      user.setMax_aday(em.element("MAX_ADAY") == null ? "无记录" : em.element("MAX_ADAY").getText());
      user.setMin_aday(em.element("MIN_ADAY") == null ? "无记录" : em.element("MIN_ADAY").getText());
      user.setCount_bs(em.element("COUNT_BS") == null ? "无记录" : em.element("COUNT_BS").getText());
      user.setSum_zje(em.element("SUM_ZJE") == null ? "无记录" : em.element("SUM_ZJE").getText());
      user.setZjedaxie(em.element("SUM_ZJE") == null ? "无记录" : MoneyUtil.toChinese(em.element("SUM_ZJE").getText()));
      user.setDqdate(tim);

      reportList.add(user);
    }
    return reportList;
  }
}